<!DOCTYPE HTML>
<html>
   <head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>知果汇</title>
		<link href="_CSS_/bootstrap.min.css" rel="stylesheet">
		<link href="_CSS_/simple-line-icons.css" rel="stylesheet">
		<link href="_CSS_/font-awesome.min.css" rel="stylesheet">
		<link href="_CSS_/jasny-bootstrap.min.css" rel="stylesheet">
		<link rel="stylesheet" href="_CSS_/nanoscroller.css">
		<link href="_CSS_/style.css" rel="stylesheet">
		<link rel="stylesheet" type="text/css" href="_CSS_/jquery-ui.css">
		<!--[if lt IE 9]>
		  <script src="js/html5shiv.min.js"></script>
		  <script src="js/respond.min.js"></script>
		<![endif]-->
		<style type="text/css">
		    body{background-color: #ffffff;}
		    .page-container{margin-bottom: 50px;}
			.row{margin-left: 0px;margin-right: 0px;min-width: 650px;}
			.radio-box{display:inline-block;width: 64px;}
			.formControls{padding-right:50px; position: relative;}
			.textarea {height: 100px;resize: none;font-size: 14px;padding: 4px;}
            .textarea-numberbar {position: absolute;right: 70px;bottom: 5px;z-index: 1;margin-bottom: 0;}
            /* label{margin-right: 15px;} */
            .formControls label{margin-right: 15px;}
            input[type=checkbox]{vertical-align: middle;margin-top: 0px;margin-right: 5px;} 
		</style>
    </head>
	<body>
	<article class="page-container">
		<form class="form form-horizontal" id="form-admin-add">
		<input type="hidden" id="type"/>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-3"></label>
			<div class="formControls col-xs-9 col-sm-9">
				
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-3"></label>
			<div class="formControls col-xs-9 col-sm-9">
				
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-3"></label>
			<div class="formControls col-xs-9 col-sm-9">
				
			</div>
		</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-3"></label>
			<div class="formControls col-xs-9 col-sm-9">
				
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-3"></label>
			<div class="formControls col-xs-9 col-sm-9">
				
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-3"></label>
			<div class="formControls col-xs-9 col-sm-9">
				
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-9 col-sm-9 col-xs-offset-3 col-sm-offset-3">
				<input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
				<input class="btn btn-cancel radius" style="color: #ffffff;background-color: #a0a0a0;" type="button" value="&nbsp;&nbsp;取消&nbsp;&nbsp;">
			</div>
		</div>
		</form>
	</article>
	
    <script src="_JS_/jquery.min.js"></script>
    <script src="_JS_/bootstrap.min.js"></script>
    <script src="_JS_/jasny-bootstrap.min.js"></script>
    <script src="_JS_/jquery.slimscroll.min.js"></script>
    <script src="_JS_/jquery.nanoscroller.min.js"></script>
    <script src="_JS_/metismenu.min.js"></script>
    <script src="_JS_/float-custom.js"></script>
    <script type="text/javascript" src="_JS_/layer/layer.js"></script>
    <script type="text/javascript" src="_JS_/jquery.validation/1.14.0/jquery.validate.min.js"></script> 
    <script type="text/javascript" src="_JS_/jquery.validation/1.14.0/validate-methods.js"></script> 
    <script type="text/javascript" src="_JS_/jquery.validation/1.14.0/messages_zh.min.js"></script> 

<script type="text/javascript">
	var url = window.location.href;
	type = url.split("#")[1];
	$("#type").val(type);
	if(type == 1){
		$(".form-label").eq(0).text('企业名称：');
		$(".form-label").eq(1).text('网址：');
		$(".form-label").eq(2).text('联系方式：');
		$(".form-label").eq(3).text('详细地址：');
		$(".form-label").eq(4).text('荣誉勋章：');
		$(".form-label").eq(5).text('企业介绍：');
		$(".formControls").eq(0).html('<input type="text" class="input-text" value="" placeholder="" id="name" name="name">');
		$(".formControls").eq(1).html('<input type="text" class="input-text" value="" placeholder="" id="internet" name="internet">');
		$(".formControls").eq(2).html('<input type="text" class="input-text" value="" placeholder="" id="phone" name="phone">');
		$(".formControls").eq(3).html('<input type="text" class="input-text" value="" placeholder="" id="address" name="address">');
		
		$.ajax({
            type: 'GET',
            url: "{:url('api/companys/companys_categorys')}",
            dataType: 'jsonp',
            jsonp: 'callback',
            success: function (msg) {
				if (msg.code == 200) {
					var html = '<dl id="role" class="permission-list"><dt><label><input type="checkbox" value="" name="user-Character-0" id="user-Character-0">勋章管理</label></dt><dd>';
					for(var i=0;i<msg.data.category.length;i++){
						html += '<dl class="cl permission-list2"><dt><label class="">'+
						'<input type="checkbox" value="'+msg.data.category[i].id+'" name="user-Character-0-0" id="user-Character-0-0">'+msg.data.category[i].name+'</label>'+
						'</dt><dd>';
						for(var j=0;j<msg.data.data.length;j++){
							if(msg.data.category[i].id+'' == msg.data.data[j].years){
							
								html += '<label class=""><input class="category" type="checkbox" value="'+msg.data.data[j].id+'" name="user-Character-0-0-0" id="user-Character-0-0-0">'+
							''+msg.data.data[j].honor+'-'+msg.data.data[j].levels+'</label>';
							}
						
						}
						
						html += '</dd></dl>';
					}
					html += '</dl>';
					$(".formControls").eq(4).html(html);
					clickfun();
				} else {
					layer.msg('失败', {icon: 5, time: 1000});
				}
			}
		})
		
		
		$(".formControls").eq(5).html('<textarea name="" cols="" rows="" class="textarea" id="introduce"  placeholder="说点什么...200个字符以内" dragonfly="true" onKeyUp="textarealength(this,200)"></textarea>'+
				'<p class="textarea-numberbar"><em class="textarea-length">0</em>/200</p>');
		
	}else if(type == 2){
		$(".form-label").eq(0).text('机构名单：');
		$(".form-label").eq(1).text('星级：');
		$(".form-label").eq(2).text('联系方式：');
		$(".form-label").eq(3).text('详细地址：');
		$(".form-label").eq(4).text('机构介绍：');
		$(".formControls").eq(0).html('<input type="text" class="input-text" value="" placeholder="" id="name" name="name">');
		$(".formControls").eq(1).html('<select id="star"><option value="0">0星</option><option value="1">1星</option><option value="2">2星</option>'+
		'<option value="3">3星</option><option value="4">4星</option><option value="5">5星</option><select>');
		$(".formControls").eq(2).html('<input type="text" class="input-text" value="" placeholder="" id="phone" name="phone">');
		$(".formControls").eq(3).html('<input type="text" class="input-text" value="" placeholder="" id="address" name="address">');
		$(".formControls").eq(4).html('<textarea name="" cols="" rows="" class="textarea" id="introduce"  placeholder="说点什么...200个字符以内" dragonfly="true" onKeyUp="textarealength(this,200)"></textarea>'+
			'<p class="textarea-numberbar"><em class="textarea-length">0</em>/200</p>');
	}else if(type == 3){
		$(".form-label").eq(0).text('代理人：');
		$(".form-label").eq(1).text('机构名单：');
		$(".form-label").eq(2).text('星级：');
		$(".form-label").eq(3).text('资格证号：');
		$(".form-label").eq(4).text('电子邮箱：');
		$(".form-label").eq(5).text('专业：');
		$(".formControls").eq(0).html('<input type="text" class="input-text" value="" placeholder="" id="username" name="username">');
		$(".formControls").eq(1).html('<input type="text" class="input-text" value="" placeholder="" id="name" name="name">');
		$(".formControls").eq(2).html('<select id="star"><option value="0">0星</option><option value="1">1星</option><option value="2">2星</option>'+
		'<option value="3">3星</option><option value="4">4星</option><option value="5">5星</option><select>');
		$(".formControls").eq(3).html('<input type="text" class="input-text" value="" placeholder="" id="certificate" name="certificate">');
		$(".formControls").eq(4).html('<input type="email" class="input-text" value="" placeholder="" id="email" name="email">');
		$(".formControls").eq(5).html('<input type="text" class="input-text" value="" placeholder="" id="major" name="major">');
	}
	




/*textarea 字数限制*/
function textarealength(obj,maxlength){
	var v = $(obj).val();
	var l = v.length;
	if( l > maxlength){
		v = v.substring(0,maxlength);
		$(obj).val(v);
	}
	$(obj).parent().find(".textarea-length").text(v.length);
}

//权限选择
    function clickfun(){
		$(".permission-list").delegate("dt input:checkbox","click",function(){
			$(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
		});

		$(".permission-list2").delegate("dt input:checkbox","click",function(){
			var lg=$(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
			var lgd=$(this).parents(".permission-list").find(".permission-list2 dd").length;
			var lgt=$(this).parents(".permission-list").find(".permission-list2 dt").find("input:checked").length;
			if(lgd==0){
			  if(lgt==0){
					$(this).parents(".permission-list").children("dt").find("input:checkbox").prop("checked",false);
				}else{
					$(this).parents(".permission-list").children("dt").find("input:checkbox").prop("checked",true);
				}
			}else{
				if(lgt==0){
				   $(this).parents(".permission-list").children("dt").find("input:checkbox").prop("checked",false);
				}else{
				   $(this).parents(".permission-list").children("dt").find("input:checkbox").prop("checked",true);
				}
			}			
		});
		
		$(".permission-list2").delegate("dd input:checkbox","click",function(){
			var l =$(this).parent().parent().find("input:checked").length;
			var l2=$(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
			if($(this).prop("checked")){
				$(this).closest("dl").find("dt input:checkbox").prop("checked",true);
				$(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",true);
			}
			else{
				if(l==0){
					$(this).closest("dl").find("dt input:checkbox").prop("checked",false);
				}
				if(l2==0){
					$(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",false);
				}
			}
		});
	}

	
	var type = $("#type").val();
	if(type == 1){
		$("#form-admin-add").validate({
			rules:{
				name:{
					required:true,
					minlength:4,
					maxlength:30
				},
				phone:{
					required:true,
				},
				address:{
					required:true,
				}
				
			},
			onkeyup:false,
			focusCleanup:true,
			success:"valid",
			submitHandler:function(form){
				//$(form).ajaxSubmit();
				
				var document = $(".category:checked");
				var role = {};
				var i = 0;
				$.each(document, function () {
					role[i] = $(this).val();
					i++;
				});
				
				var name = $("#name").val();
				var internet = $("#internet").val();
				var phone = $("#phone").val();
				var address = $("#address").val();
				var textarea = $("#introduce").val();
				$.ajax({
					type: "post",
					url: "{:url('api/companys/companys_add')}",
					data: {
						'type':type,
						'internet':internet,
						'name': name,
						'phone': phone,
						'address': address,
						'introduce': textarea,
						'role': role,
					},
					dataType: "json",
					beforeSend: function () {
						layer.load(1, {
							shade: [0.2, '#666'],
							offset: ['50%', '50%'],
						});
					},
					success: function (msg) {
						layer.close(layer.index);
						if(msg.code == '200'){
							parent.postfile(1,1);
						}else{
							layer.msg(msg.message);
						}
					}
				})
			}
		});
	}else if(type == 2){
		$("#form-admin-add").validate({
			rules:{
				name:{
					required:true,
					minlength:4,
					maxlength:30
				},
				phone:{
					required:true,
				},
				address:{
					required:true,
				}
				
			},
			onkeyup:false,
			focusCleanup:true,
			success:"valid",
			submitHandler:function(form){
				//$(form).ajaxSubmit();
				var name = $("#name").val();
				var star = $("#star").val();
				var phone = $("#phone").val();
				var address = $("#address").val();
				var textarea = $("#introduce").val();
				$.ajax({
					type: "post",
					url: "{:url('api/companys/companys_add')}",
					data: {
						'type':type,
						'star': star,
						'name': name,
						'phone': phone,
						'address': address,
						'introduce': textarea,
					},
					dataType: "json",
					beforeSend: function () {
						layer.load(1, {
							shade: [0.2, '#666'],
							offset: ['50%', '50%'],
						});
					},
					success: function (msg) {
						layer.close(layer.index);
						if(msg.code == '200'){
							parent.postfile(1,2);
						}else{
							layer.msg(msg.message);
						}
					}
				})
			}
		});
	}else if(type == 3){
		$("#form-admin-add").validate({
			rules:{
				username:{
					required:true,
				},
				name:{
					required:true,
					minlength:4,
					maxlength:30
				},
				certificate:{
					required:true,
				},
				email:{
					required:true,
					email:true,
				},
				major:{
					required:true,
				}
				
			},
			onkeyup:false,
			focusCleanup:true,
			success:"valid",
			submitHandler:function(form){
				//$(form).ajaxSubmit();
				
				
				var username = $("#username").val();
				var name = $("#name").val();
				var certificate = $("#certificate").val();
				var email = $("#email").val();
				var major = $("#major").val();
				var star = $("#star").val();
				$.ajax({
					type: "post",
					url: "{:url('api/companys/companys_add')}",
					data: {
						'type':type,
						'username': username,
						'name': name,
						'certificate': certificate,
						'email': email,
						'major': major,
						'star': star,
					},
					dataType: "json",
					beforeSend: function () {
						layer.load(1, {
							shade: [0.2, '#666'],
							offset: ['50%', '50%'],
						});
					},
					success: function (msg) {
						layer.close(layer.index);
						if(msg.code == '200'){
							parent.postfile(1,3);
						}else{
							layer.msg(msg.message);
						}
					}
				})
			}
		});
	}
</script> 
</body>
</html>